package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Long
 * @since 2021-12-23
 */
@Controller
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserMapper mapper;

    @RequestMapping("/hhh")
    public String gethhh(Model model){
        List<User> users=userService.list();
        model.addAttribute("li",users);
        return "index";
    }

    @RequestMapping("/page")
    public String page(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                       @RequestParam(value = "pageSize",required = false,defaultValue = "2")Integer pageSize,
                       Model model){
        Page<User> page = userService.page(new Page<>(pageNum, pageSize));
        model.addAttribute("li",page);
        return "index";
    }

    @RequestMapping("/update")
    public String update(Long id,Model model){
        User user=userService.getById(id);
        model.addAttribute("user",user);
        model.addAttribute("pan","update1");
        return "update";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        int i=mapper.deleteById(id);
        return "redirect:/page";
    }

    @RequestMapping("/update1")
    public String update1(Integer id,String name,Integer age,String email){
        User user=mapper.selectById(id);
        user.setName(name);
        user.setAge(age);
        user.setEmail(email);
        int i = mapper.updateById(user);
        System.out.println(i);
        return "redirect:/page";
    }

    @RequestMapping("/add")
    public String add(Model model){
        model.addAttribute("pan","add1");
        return "update";
    }

    @RequestMapping("/add1")
    public String add1(Integer id,String name,Integer age,String email,Model model){
        User user=new User();
//        user.setId(16l);
        user.setName(name);
        user.setAge(age);
        user.setEmail(email);

        int i=mapper.insert(user);
        System.out.println(i);
        return "redirect:/page";
    }
}

